//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Microsoft.TridentWordAddIn.Presentation
{
    /// <summary>
    /// Custom label control which displays required field icon at the end of text, if IsRequired field is set to true.
    /// </summary>
    public class CustomLabel : Label
    {
        private bool isRequiredField = false;
        private Font requiredFont;

        /// <summary>
        /// Initializes a new instance of the <see cref="CustomLabel"/> class.
        /// </summary>
        public CustomLabel()
        {
            this.AutoEllipsis = true;
            this.ForeColor = Color.Black;
            this.requiredFont = base.Font;
            this.requiredFont = new Font(base.Font.FontFamily, 11.5F, FontStyle.Bold, GraphicsUnit.Point);
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is required field.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is required field; otherwise, <c>false</c>.
        /// </value>
        [Browsable(true)]
        [Description("Display required field icon.")]
        public bool IsRequiredField
        {
            get
            {
                return this.isRequiredField;
            }
            set
            {
                this.isRequiredField = value;
                Invalidate();
            }
        }

        /// <summary>
        /// Raises the <see cref="E:Paint"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (this.isRequiredField)
            {
                SizeF sz = e.Graphics.MeasureString(base.Text, base.Font);
                int requiredWidth = this.Width < sz.Width ? this.Width - 10 : (int)sz.Width - 5;
                Rectangle txtRect = new Rectangle(requiredWidth, -3, 10, 10);
                e.Graphics.DrawString("*", this.requiredFont, new SolidBrush(Color.Red), txtRect);
            }
        }
    }
}
